{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0pKllbPyK_BC"
      },
      "source": [
        "## **Applio NoUI**\n",
        "A simple, high-quality voice conversion tool focused on ease of use and performance.\n",
        "\n",
        "[Support](https://discord.gg/urxFjYmYYh) — [GitHub](https://github.com/IAHispano/Applio) — [Terms of Use](https://github.com/IAHispano/Applio/blob/main/TERMS_OF_USE.md)\n",
        "\n",
        "<br>\n",
        "\n",
        "---\n",
        "\n",
        "<br>\n",
        "\n",
        "#### **Acknowledgments**\n",
        "\n",
        "To all external collaborators for their special help in the following areas:\n",
        "* Hina (Encryption method)\n",
        "* Poopmaster (Extra section)\n",
        "* Shirou (UV installer)\n",
        "* Bruno5430 (AutoBackup code and general notebook maintenance)\n",
        "\n",
        "#### **Disclaimer**\n",
        "By using Applio, you agree to comply with ethical and legal standards, respect intellectual property and privacy rights, avoid harmful or prohibited uses, and accept full responsibility for any outcomes, while Applio disclaims liability and reserves the right to amend these terms."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "ymMCTSD6m8qV"
      },
      "source": [
        "### **Install Applio**\n",
        "If the runtime restarts, re-run the installation steps."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "yFhAeKGOp9aa"
      },
      "outputs": [],
      "source": [
        "# @title Mount Google Drive\n",
        "from google.colab import drive\n",
        "from google.colab._message import MessageError\n",
        "\n",
        "try:\n",
        "  drive.mount(\"/content/drive\")\n",
        "except MessageError:\n",
        "  print(\"❌ Failed to mount drive\")\n",
        "\n",
        "# Migrate folders to match documentation\n",
        "from pathlib import Path\n",
        "if Path(\"/content/drive\").is_mount():\n",
        "  %cd \"/content/drive/MyDrive/\"\n",
        "  if not Path(\"ApplioBackup/\").exists() and Path(\"RVC_Backup/\").exists():\n",
        "    !mv \"RVC_Backup/\" \"ApplioBackup/\"\n",
        "  %cd /content"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "CAXW55BQm0PP"
      },
      "outputs": [],
      "source": [
        "# @title Setup runtime environment\n",
        "from multiprocessing import cpu_count\n",
        "cpu_cores = cpu_count()\n",
        "post_process = False\n",
        "LOGS_PATH = \"/content/Applio/logs\"\n",
        "BACKUPS_PATH = \"/content/drive/MyDrive/ApplioBackup\"\n",
        "\n",
        "%cd /content\n",
        "!git config --global advice.detachedHead false\n",
        "!git clone https://github.com/IAHispano/Applio --branch 3.6.0 --single-branch\n",
        "%cd /content/Applio\n",
        "\n",
        "# Install older python\n",
        "!apt update -y\n",
        "!apt install -y python3.11 python3.11-distutils python3.11-dev portaudio19-dev\n",
        "!update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2\n",
        "!update-alternatives --set python3 /usr/bin/python3.11\n",
        "from sys import path\n",
        "path.append('/usr/local/lib/python3.11/dist-packages')\n",
        "\n",
        "print(\"Installing requirements...\")\n",
        "!curl -LsSf https://astral.sh/uv/install.sh | sh\n",
        "!uv pip install -q -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cu128 --index-strategy unsafe-best-match\n",
        "!uv pip install -q jupyter-ui-poll\n",
        "!python core.py \"prerequisites\" --models \"True\" --pretraineds_hifigan \"True\"\n",
        "print(\"Finished installing requirements!\")\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YzaeMYsUE97Y"
      },
      "source": [
        "### **Infer**\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "2miFQtlfiWy_"
      },
      "outputs": [],
      "source": [
        "# @title Sync with Google Drive\n",
        "# @markdown 💾 Run this cell to automatically Save/Load models from your mounted drive\n",
        "# @title\n",
        "# @markdown This will merge and link your `ApplioBackup` folder from gdrive to this notebook\n",
        "from IPython.display import display, clear_output\n",
        "from pathlib import Path\n",
        "\n",
        "non_bak_folders = [\"mute\", \"reference\", \"zips\", \"mute_spin\", \"mute_spin-v2\"]\n",
        "non_bak_path = \"/tmp/rvc_logs\"\n",
        "\n",
        "\n",
        "def press_button(button):\n",
        "  button.disabled = True\n",
        "\n",
        "\n",
        "def get_date(path: Path):\n",
        "  from datetime import datetime\n",
        "  return datetime.fromtimestamp(int(path.stat().st_mtime))\n",
        "\n",
        "\n",
        "def get_size(path: Path):\n",
        "  !du -shx --apparent-size \"{path}\" > /tmp/size.txt\n",
        "  return open(\"/tmp/size.txt\").readlines().pop(0).split(\"\t\")[0] + \"B\"\n",
        "\n",
        "\n",
        "def sync_folders(folder: Path, backup: Path):\n",
        "  from ipywidgets import widgets\n",
        "  from jupyter_ui_poll import ui_events\n",
        "  from time import sleep\n",
        "\n",
        "  local = widgets.VBox([\n",
        "      widgets.Label(f\"Local: {LOGS_PATH.removeprefix('/content/')}/{folder.name}/\"),\n",
        "      widgets.Label(f\"Size: {get_size(folder)}\"),\n",
        "      widgets.Label(f\"Last modified: {get_date(folder)}\")\n",
        "  ])\n",
        "  remote = widgets.VBox([\n",
        "      widgets.Label(f\"Remote: {BACKUPS_PATH.removeprefix('/content/')}/{backup.name}/\"),\n",
        "      widgets.Label(f\"Size: {get_size(backup)}\"),\n",
        "      widgets.Label(f\"Last modified: {get_date(backup)}\")\n",
        "  ])\n",
        "  separator = widgets.VBox([\n",
        "      widgets.Label(\"|||\"),\n",
        "      widgets.Label(\"|||\"),\n",
        "      widgets.Label(\"|||\")\n",
        "  ])\n",
        "  radio = widgets.RadioButtons(\n",
        "      options=[\n",
        "          \"Save local model to drive\",\n",
        "          \"Keep remote model\"\n",
        "      ]\n",
        "  )\n",
        "  button = widgets.Button(\n",
        "      description=\"Sync\",\n",
        "      icon=\"upload\",\n",
        "      tooltip=\"Sync model\"\n",
        "  )\n",
        "  button.on_click(press_button)\n",
        "\n",
        "  clear_output()\n",
        "  print(f\"Your local model '{folder.name}' is in conflict with it's copy in Google Drive.\")\n",
        "  print(\"Please select which one you want to keep:\")\n",
        "  display(widgets.Box([local, separator, remote]))\n",
        "  display(radio)\n",
        "  display(button)\n",
        "\n",
        "  with ui_events() as poll:\n",
        "    while not button.disabled:\n",
        "      poll(10)\n",
        "      sleep(0.1)\n",
        "\n",
        "  match radio.value:\n",
        "    case \"Save local model to drive\":\n",
        "      !rm -r \"{backup}\"\n",
        "      !mv \"{folder}\" \"{backup}\"\n",
        "    case \"Keep remote model\":\n",
        "      !rm -r \"{folder}\"\n",
        "\n",
        "\n",
        "if Path(\"/content/drive\").is_mount():\n",
        "  !mkdir -p \"{BACKUPS_PATH}\"\n",
        "  !mkdir -p \"{non_bak_path}\"\n",
        "\n",
        "  if not Path(LOGS_PATH).is_symlink():\n",
        "    for folder in non_bak_folders:\n",
        "      folder = Path(f\"{LOGS_PATH}/{folder}\")\n",
        "      backup = Path(f\"{BACKUPS_PATH}/{folder.name}\")\n",
        "\n",
        "      !mkdir -p \"{folder}\"\n",
        "      !mv \"{folder}\" \"{non_bak_path}\" &> /dev/null\n",
        "      !rm -rf \"{folder}\"\n",
        "      folder = Path(f\"{non_bak_path}/{folder.name}\")\n",
        "      if backup.exists() and backup.resolve() != folder.resolve():\n",
        "        !rm -r \"{backup}\"\n",
        "      !ln -s \"{folder}\" \"{backup}\" &> /dev/null\n",
        "\n",
        "    for model in Path(LOGS_PATH).iterdir():\n",
        "      if model.is_dir() and not model.is_symlink():\n",
        "        backup = Path(f\"{BACKUPS_PATH}/{model.name}\")\n",
        "\n",
        "        if model.name == \".ipynb_checkpoints\":\n",
        "          continue\n",
        "\n",
        "        if backup.exists() and backup.is_dir():\n",
        "          sync_folders(model, backup)\n",
        "        else:\n",
        "          !rm \"{backup}\"\n",
        "          !mv \"{model}\" \"{backup}\"\n",
        "\n",
        "    !rm -r \"{LOGS_PATH}\"\n",
        "    !ln -s \"{BACKUPS_PATH}\" \"{LOGS_PATH}\"\n",
        "\n",
        "    clear_output()\n",
        "    print(\"✅ Models are synced!\")\n",
        "\n",
        "  else:\n",
        "    !rm \"{LOGS_PATH}\"\n",
        "    !ln -s \"{BACKUPS_PATH}\" \"{LOGS_PATH}\"\n",
        "    clear_output()\n",
        "    print(\"✅ Models already synced!\")\n",
        "\n",
        "else:\n",
        "  print(\"❌ Drive is not mounted, skipping model syncing\")\n",
        "  print(\"To sync your models, first mount your Google Drive and re-run this cell\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "v0EgikgjFCjE"
      },
      "outputs": [],
      "source": [
        "# @title Download model\n",
        "# @markdown Hugging Face or Google Drive\n",
        "model_link = \"https://huggingface.co/Darwin/Darwin/resolve/main/Darwin.zip\"  # @param {type:\"string\"}\n",
        "\n",
        "%cd /content/Applio\n",
        "!python core.py download --model_link \"{model_link}\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "lrCKEOzvDPRu"
      },
      "outputs": [],
      "source": [
        "# @title Run Inference\n",
        "# @markdown Please upload the audio file to your Google Drive path `/content/drive/MyDrive` and specify its name here. For the model name, use the zip file name without the extension. Alternatively, you can check the path `/content/Applio/logs` for the model name (name of the folder).\n",
        "%cd /content/Applio\n",
        "from pathlib import Path\n",
        "\n",
        "model_name = \"Darwin\"  # @param {type:\"string\"}\n",
        "model_path = Path(f\"{LOGS_PATH}/{model_name}\")\n",
        "if not (model_path.exists() and model_path.is_dir()):\n",
        "    raise FileNotFoundError(f\"Model directory not found: {model_path.resolve()}\")\n",
        "\n",
        "# Select either the last checkpoint or the final weight\n",
        "!ls -t \"{model_path}\"/\"{model_name}\"_*e_*s.pth \"{model_path}\"/\"{model_name}\".pth 2> /dev/null | head -n 1 > /tmp/pth.txt\n",
        "pth_file = open(\"/tmp/pth.txt\", \"r\").read().strip()\n",
        "if pth_file == \"\":\n",
        "  raise FileNotFoundError(f\"No model weight found in directory: {model_path.resolve()}\\nMake sure that the file is properly named (e.g. \\\"{model_name}.pth)\\\"\")\n",
        "\n",
        "!ls -t \"{model_path}\"/*.index | head -n 1 > /tmp/index.txt\n",
        "index_file = open(\"/tmp/index.txt\", \"r\").read().strip()\n",
        "\n",
        "input_path = \"/content/example.wav\"  # @param {type:\"string\"}\n",
        "output_path = \"/content/output.wav\"\n",
        "export_format = \"WAV\"  # @param ['WAV', 'MP3', 'FLAC', 'OGG', 'M4A'] {allow-input: false}\n",
        "f0_method = \"rmvpe\"  # @param [\"crepe\", \"crepe-tiny\", \"rmvpe\", \"fcpe\", \"hybrid[rmvpe+fcpe]\"] {allow-input: false}\n",
        "f0_up_key = 0  # @param {type:\"slider\", min:-24, max:24, step:0}\n",
        "rms_mix_rate = 0.8  # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "protect = 0.5  # @param {type:\"slider\", min:0.0, max:0.5, step:0.1}\n",
        "index_rate = 0.7  # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "clean_strength = 0.7  # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "split_audio = False  # @param{type:\"boolean\"}\n",
        "clean_audio = False  # @param{type:\"boolean\"}\n",
        "f0_autotune = False  # @param{type:\"boolean\"}\n",
        "formant_shift = False # @param{type:\"boolean\"}\n",
        "formant_qfrency = 1.0 # @param {type:\"slider\", min:1.0, max:16.0, step:0.1}\n",
        "formant_timbre = 1.0 # @param {type:\"slider\", min:1.0, max:16.0, step:0.1}\n",
        "embedder_model = \"contentvec\" # @param [\"contentvec\", \"chinese-hubert-base\", \"japanese-hubert-base\", \"korean-hubert-base\", \"custom\"] {allow-input: false}\n",
        "embedder_model_custom = \"\" # @param {type:\"string\"}\n",
        "\n",
        "!rm -f \"{output_path}\"\n",
        "if post_process:\n",
        "  !python core.py infer --pitch \"{f0_up_key}\" --volume_envelope \"{rms_mix_rate}\" --index_rate \"{index_rate}\" --protect \"{protect}\" --f0_autotune \"{f0_autotune}\" --f0_method \"{f0_method}\" --input_path \"{input_path}\" --output_path \"{output_path}\" --pth_path \"{pth_file}\" --index_path \"{index_file}\" --split_audio \"{split_audio}\" --clean_audio \"{clean_audio}\" --clean_strength \"{clean_strength}\" --export_format \"{export_format}\" --embedder_model \"{embedder_model}\" --embedder_model_custom \"{embedder_model_custom}\" --formant_shifting \"{formant_shift}\" --formant_qfrency \"{formant_qfrency}\" --formant_timbre \"{formant_timbre}\" --post_process \"{post_process}\" --reverb \"{reverb}\" --pitch_shift \"{pitch_shift}\" --limiter \"{limiter}\" --gain \"{gain}\" --distortion \"{distortion}\" --chorus \"{chorus}\" --bitcrush \"{bitcrush}\" --clipping \"{clipping}\" --compressor \"{compressor}\" --delay \"{delay}\" --reverb_room_size \"{reverb_room_size}\" --reverb_damping \"{reverb_damping}\" --reverb_wet_gain \"{reverb_wet_gain}\" --reverb_dry_gain \"{reverb_dry_gain}\" --reverb_width \"{reverb_width}\" --reverb_freeze_mode \"{reverb_freeze_mode}\" --pitch_shift_semitones \"{pitch_shift_semitones}\" --limiter_threshold \"{limiter_threshold}\" --limiter_release_time \"{limiter_release_time}\" --gain_db \"{gain_db}\" --distortion_gain \"{distortion_gain}\" --chorus_rate \"{chorus_rate}\" --chorus_depth \"{chorus_depth}\" --chorus_center_delay \"{chorus_center_delay}\" --chorus_feedback \"{chorus_feedback}\" --chorus_mix \"{chorus_mix}\" --bitcrush_bit_depth \"{bitcrush_bit_depth}\" --clipping_threshold \"{clipping_threshold}\" --compressor_threshold \"{compressor_threshold}\" --compressor_ratio \"{compressor_ratio}\" --compressor_attack \"{compressor_attack}\" --compressor_release \"{compressor_release}\" --delay_seconds \"{delay_seconds}\" --delay_feedback \"{delay_feedback}\" --delay_mix \"{delay_mix}\"\n",
        "else:\n",
        "  !python core.py infer --pitch \"{f0_up_key}\" --volume_envelope \"{rms_mix_rate}\" --index_rate \"{index_rate}\" --protect \"{protect}\" --f0_autotune \"{f0_autotune}\" --f0_method \"{f0_method}\" --input_path \"{input_path}\" --output_path \"{output_path}\" --pth_path \"{pth_file}\" --index_path \"{index_file}\" --split_audio \"{split_audio}\" --clean_audio \"{clean_audio}\" --clean_strength \"{clean_strength}\" --export_format \"{export_format}\" --embedder_model \"{embedder_model}\" --embedder_model_custom \"{embedder_model_custom}\" --formant_shifting \"{formant_shift}\" --formant_qfrency \"{formant_qfrency}\" --formant_timbre \"{formant_timbre}\" --post_process \"{post_process}\"\n",
        "\n",
        "if Path(output_path).exists():\n",
        "  from IPython.display import Audio, display\n",
        "  output_path = output_path.replace(\".wav\", f\".{export_format.lower()}\")\n",
        "  display(Audio(output_path, autoplay=True))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "J43qejJ-2Tpp"
      },
      "outputs": [],
      "source": [
        "# @title Enable post-processing effects for inference\n",
        "post_process = False # @param{type:\"boolean\"}\n",
        "reverb = False # @param{type:\"boolean\"}\n",
        "pitch_shift = False # @param{type:\"boolean\"}\n",
        "limiter = False # @param{type:\"boolean\"}\n",
        "gain = False # @param{type:\"boolean\"}\n",
        "distortion = False # @param{type:\"boolean\"}\n",
        "chorus = False # @param{type:\"boolean\"}\n",
        "bitcrush = False # @param{type:\"boolean\"}\n",
        "clipping = False # @param{type:\"boolean\"}\n",
        "compressor = False # @param{type:\"boolean\"}\n",
        "delay = False # @param{type:\"boolean\"}\n",
        "\n",
        "reverb_room_size = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "reverb_damping = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "reverb_wet_gain = 0.0 # @param {type:\"slider\", min:-20.0, max:20.0, step:0.1}\n",
        "reverb_dry_gain = 0.0 # @param {type:\"slider\", min:-20.0, max:20.0, step:0.1}\n",
        "reverb_width = 1.0 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "reverb_freeze_mode = 0.0 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "\n",
        "pitch_shift_semitones = 0.0 # @param {type:\"slider\", min:-12.0, max:12.0, step:0.1}\n",
        "\n",
        "limiter_threshold = -1.0 # @param {type:\"slider\", min:-20.0, max:0.0, step:0.1}\n",
        "limiter_release_time = 0.05 # @param {type:\"slider\", min:0.0, max:1.0, step:0.01}\n",
        "\n",
        "gain_db = 0.0 # @param {type:\"slider\", min:-20.0, max:20.0, step:0.1}\n",
        "\n",
        "distortion_gain = 0.0 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "\n",
        "chorus_rate = 1.5 # @param {type:\"slider\", min:0.1, max:10.0, step:0.1}\n",
        "chorus_depth = 0.1 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "chorus_center_delay = 15.0 # @param {type:\"slider\", min:0.0, max:50.0, step:0.1}\n",
        "chorus_feedback = 0.25 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "chorus_mix = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "\n",
        "bitcrush_bit_depth = 4 # @param {type:\"slider\", min:1, max:16, step:1}\n",
        "\n",
        "clipping_threshold = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "\n",
        "compressor_threshold = -20.0 # @param {type:\"slider\", min:-60.0, max:0.0, step:0.1}\n",
        "compressor_ratio = 4.0 # @param {type:\"slider\", min:1.0, max:20.0, step:0.1}\n",
        "compressor_attack = 0.001 # @param {type:\"slider\", min:0.0, max:0.1, step:0.001}\n",
        "compressor_release = 0.1 # @param {type:\"slider\", min:0.0, max:1.0, step:0.01}\n",
        "\n",
        "delay_seconds = 0.1 # @param {type:\"slider\", min:0.0, max:1.0, step:0.01}\n",
        "delay_feedback = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
        "delay_mix = 0.5 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1QkabnLlF2KB"
      },
      "source": [
        "### **Train**"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "64V5TWxp05cn"
      },
      "outputs": [],
      "source": [
        "# @title Setup model parameters\n",
        "\n",
        "model_name = \"Darwin\" # @param {type:\"string\"}\n",
        "sample_rate = \"40k\"  # @param [\"32k\", \"40k\", \"48k\"] {allow-input: false}\n",
        "sr = int(sample_rate.rstrip(\"k\")) * 1000\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "oBzqm4JkGGa0"
      },
      "outputs": [],
      "source": [
        "# @title Preprocess Dataset\n",
        "\n",
        "dataset_path = \"/content/drive/MyDrive/Darwin_Dataset\"  # @param {type:\"string\"}\n",
        "\n",
        "cut_preprocess = \"Automatic\" # @param [\"Skip\",\"Simple\",\"Automatic\"]\n",
        "chunk_len = 3 # @param {type:\"slider\", min:0.5, max:5.0, step:0.5}\n",
        "overlap_len = 0.3 # @param {type:\"slider\", min:0, max:0.5, step:0.1}\n",
        "process_effects = False # @param{type:\"boolean\"}\n",
        "noise_reduction = False # @param{type:\"boolean\"}\n",
        "noise_reduction_strength = 0.7 # @param {type:\"slider\", min:0.0, max:1.0, step:0.1}\n",
		"normalization_mode = \"none\" # @param [\"none\",\"pre\",\"post\"]\n",
        "\n",
        "%cd /content/Applio\n",
        "!python core.py preprocess --model_name \"{model_name}\" --dataset_path \"{dataset_path}\" --sample_rate \"{sr}\" --cpu_cores \"{cpu_cores}\" --cut_preprocess \"{cut_preprocess}\" --process_effects \"{process_effects}\" --noise_reduction \"{noise_reduction}\" --noise_reduction_strength \"{noise_reduction_strength}\" --chunk_len \"{chunk_len}\" --overlap_len \"{overlap_len}\" --normalization_mode \"{normalization_mode}\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "zWMiMYfRJTJv"
      },
      "outputs": [],
      "source": [
        "# @title Extract Features\n",
        "f0_method = \"rmvpe\"  # @param [\"crepe\", \"crepe-tiny\", \"rmvpe\"] {allow-input: false}\n",
        "\n",
        "sr = int(sample_rate.rstrip(\"k\")) * 1000\n",
        "include_mutes = 2 # @param {type:\"slider\", min:0, max:10, step:1}\n",
        "embedder_model = \"contentvec\" # @param [\"contentvec\", \"chinese-hubert-base\", \"japanese-hubert-base\", \"korean-hubert-base\", \"custom\"] {allow-input: false}\n",
        "embedder_model_custom = \"\" # @param {type:\"string\"}\n",
        "\n",
        "%cd /content/Applio\n",
        "!python core.py extract --model_name \"{model_name}\" --f0_method \"{f0_method}\" --sample_rate \"{sr}\" --cpu_cores \"{cpu_cores}\" --gpu \"0\" --embedder_model \"{embedder_model}\" --embedder_model_custom \"{embedder_model_custom}\"  --include_mutes \"{include_mutes}\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "bHLs5AT4Q1ck"
      },
      "outputs": [],
      "source": [
        "# @title Generate index file\n",
        "index_algorithm = \"Auto\"  # @param [\"Auto\", \"Faiss\", \"KMeans\"] {allow-input: false}\n",
        "\n",
        "%cd /content/Applio\n",
        "!python core.py index --model_name \"{model_name}\" --index_algorithm \"{index_algorithm}\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "TI6LLdIzKAIa"
      },
      "outputs": [],
      "source": [
        "# @title Start Training\n",
        "# @markdown ### ⚙️ Train Settings\n",
        "total_epoch = 800  # @param {type:\"integer\"}\n",
        "batch_size = 8  # @param {type:\"slider\", min:1, max:25, step:0}\n",
        "pretrained = True  # @param{type:\"boolean\"}\n",
        "cleanup = False  # @param{type:\"boolean\"}\n",
        "cache_data_in_gpu = False  # @param{type:\"boolean\"}\n",
        "vocoder = \"HiFi-GAN\" # @param [\"HiFi-GAN\"]\n",
        "checkpointing = False\n",
        "tensorboard = True  # @param{type:\"boolean\"}\n",
        "# @markdown ### ➡️ Choose how many epochs your model will be stored\n",
        "save_every_epoch = 10  # @param {type:\"slider\", min:1, max:100, step:0}\n",
        "save_only_latest = True  # @param{type:\"boolean\"}\n",
        "save_every_weights = False  # @param{type:\"boolean\"}\n",
        "overtraining_detector = False  # @param{type:\"boolean\"}\n",
        "overtraining_threshold = 50  # @param {type:\"slider\", min:1, max:100, step:0}\n",
        "# @markdown ### ❓ Optional\n",
        "# @markdown In case you select custom pretrained, you will have to download the pretraineds and enter the path of the pretraineds.\n",
        "custom_pretrained = False  # @param{type:\"boolean\"}\n",
        "g_pretrained_path = \"/content/Applio/rvc/models/pretraineds/pretraineds_custom/G48k.pth\"  # @param {type:\"string\"}\n",
        "d_pretrained_path = \"/content/Applio/rvc/models/pretraineds/pretraineds_custom/D48k.pth\"  # @param {type:\"string\"}\n",
        "\n",
        "\n",
        "%cd /content/Applio\n",
        "if tensorboard:\n",
        "  %load_ext tensorboard\n",
        "  %tensorboard --logdir logs --bind_all\n",
        "!python core.py train --model_name \"{model_name}\" --save_every_epoch \"{save_every_epoch}\" --save_only_latest \"{save_only_latest}\" --save_every_weights \"{save_every_weights}\" --total_epoch \"{total_epoch}\" --sample_rate \"{sr}\" --batch_size \"{batch_size}\" --gpu 0 --pretrained \"{pretrained}\" --custom_pretrained \"{custom_pretrained}\" --g_pretrained_path \"{g_pretrained_path}\" --d_pretrained_path \"{d_pretrained_path}\" --overtraining_detector \"{overtraining_detector}\" --overtraining_threshold \"{overtraining_threshold}\" --cleanup \"{cleanup}\" --cache_data_in_gpu \"{cache_data_in_gpu}\" --vocoder \"{vocoder}\" --checkpointing \"{checkpointing}\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "yNjSvyQiOe7R"
      },
      "outputs": [],
      "source": [
        "# @title Download Pretraineds\n",
        "# @markdown Downloads the G/D files and generates the paths needed for the \"Start Training\" section.\n",
        "from IPython.display import clear_output\n",
        "\n",
        "G = \"https://.../G.pth\" # @param {type:\"string\"}\n",
        "D = \"https://.../D.pth\" # @param {type:\"string\"}\n",
        "\n",
        "import os\n",
        "import requests\n",
        "from tqdm import tqdm\n",
        "\n",
        "def download(url, text):\n",
        "    if not url:\n",
        "        return None\n",
        "\n",
        "    dest_dir = \"/content/Applio/rvc/models/pretraineds/custom\"\n",
        "    os.makedirs(dest_dir, exist_ok=True)\n",
        "\n",
        "    filename = os.path.basename(url)\n",
        "    dest_path = os.path.join(dest_dir, filename)\n",
        "\n",
        "    print(f\"Downloading {text}: {filename}...\")\n",
        "\n",
        "    try:\n",
        "        response = requests.get(url, stream=True)\n",
        "        response.raise_for_status()\n",
        "        total_size = int(response.headers.get('content-length', 0))\n",
        "\n",
        "        with open(dest_path, 'wb') as f, tqdm(\n",
        "            total=total_size, unit='iB', unit_scale=True, unit_divisor=1024\n",
        "        ) as bar:\n",
        "            for chunk in response.iter_content(chunk_size=8192):\n",
        "                size = f.write(chunk)\n",
        "                bar.update(size)\n",
        "        return dest_path\n",
        "    except Exception as e:\n",
        "        print(f\"Error downloading {text}: {e}\")\n",
        "        return None\n",
        "\n",
        "path_g = download(G.replace(\"?download=true\", \"\"), \"G\")\n",
        "path_d = download(D.replace(\"?download=true\", \"\"), \"D\")\n",
        "clear_output()\n",
        "if path_g:\n",
        "    print(f\"G Path: {path_g}\")\n",
        "else:\n",
        "    print(\"No G model downloaded.\")\n",
        "\n",
        "if path_d:\n",
        "    print(f\"D Path: {path_d}\")\n",
        "else:\n",
        "    print(\"No D model downloaded.\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "cellView": "form",
        "id": "X_eU_SoiHIQg"
      },
      "outputs": [],
      "source": [
        "# @title Export model\n",
        "# @markdown Export model to a zip file\n",
        "# @markdown * Training: Bigger file size, can continue training\n",
        "# @markdown * Inference: Smaller file size, only for model inference\n",
        "# @title\n",
        "# @markdown (Note) Exporting for training is only recommended for use outside of Applio, if you plan to resume training later, use [Sync with Google Drive](#scrollTo=2miFQtlfiWy_) cell instead.\n",
        "EXPORT_PATH = \"/content/drive/MyDrive/ApplioExported\"\n",
        "from pathlib import Path\n",
        "\n",
        "export_for = \"inference\" # @param [\"training\", \"inference\"] {allow-input: false}\n",
        "\n",
        "logs_folder = Path(f\"/content/Applio/logs/{model_name}/\")\n",
        "if not (logs_folder.exists() and logs_folder.is_dir()):\n",
        "    raise FileNotFoundError(f\"{model_name} model folder not found\")\n",
        "\n",
        "%cd {logs_folder}/..\n",
        "if export_for == \"training\":\n",
        "  !zip -r \"/content/{model_name}.zip\" \"{model_name}\"\n",
        "else:\n",
        "  # find latest trained weight file\n",
        "  !ls -t \"{model_name}/{model_name}\"_*e_*s.pth | head -n 1 > /tmp/weight.txt\n",
        "  weight_path = open(\"/tmp/weight.txt\", \"r\").read().strip()\n",
        "  if weight_path == \"\":\n",
        "    raise FileNotFoundError(\"Model has no weight file, please finish training first\")\n",
        "  weight_name = Path(weight_path).name\n",
        "  # command does not fail if index is missing, this is intended\n",
        "  !zip \"/content/{model_name}.zip\" \"{model_name}/{weight_name}\" \"{model_name}/{model_name}.index\"\n",
        "\n",
        "if Path(\"/content/drive\").is_mount():\n",
        "  !mkdir -p \"{EXPORT_PATH}\"\n",
        "  !mv \"/content/{model_name}.zip\" \"{EXPORT_PATH}\" && echo \"Exported model to {EXPORT_PATH}/{model_name}.zip\"\n",
        "else:\n",
        "  !echo \"Drive not mounted, exporting model to /content/{model_name}.zip\""
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [
        "ymMCTSD6m8qV"
      ],
      "private_outputs": true,
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
